/*
  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.

  SpringProx API
  --------------

  Copyright (c) 2000-2008 SpringCard - www.springcard.com

  sprox_api_i.h
  --------------
  Common header included by each source file of the library.

*/

#ifndef SPROX_API_I_H
#define SPROX_API_I_H

#ifndef SPROX_API_REENTRANT
  /*
   * We are building the classical SpringProx API
   * --------------------------------------------
   */

  /* Function are named SPROX_.... */
  #define SPROX_API_FUNC(a)        SPRINGPROX_LIB SWORD SPRINGPROX_API SPROX_ ## a
  #define SPROX_API_FUNC_T(a, b)   SPRINGPROX_LIB a     SPRINGPROX_API SPROX_ ## b

  /* Redirect function calls to this name */
  #define SPROX_API_CALL(a)  SPROX_ ## a  

  /* The instance parameter doesn't exist */
  #define SPROX_PARAM
  #define SPROX_PARAM_V      void
  #define SPROX_PARAM_P
  #define SPROX_PARAM_PV
  
  /* When needed the instance is emulated by a pointer to a global variable */
  #define SPROX_PARAM_TO_CTX SPROX_CTX_ST *sprox_ctx = sprox_ctx_glob;
  
  /* This is the low-level dialog function */ 
  #define SPROX_DLG_FUNC     SPROX_Function
  #define SPROX_WRS_FUNC     SPROX_FunctionWaitResp

#else
  /*
   * We are building the SpringProx "EX" API
   * ---------------------------------------
   */

  /* Function are renamed SPROXx_.... */
  #define SPROX_API_FUNC(a)        SPRINGPROX_LIB SWORD SPRINGPROX_API SPROXx_ ## a
  #define SPROX_API_FUNC_T(a, b)   SPRINGPROX_LIB a     SPRINGPROX_API SPROXx_ ## b

  /* Redirect function calls to this name */
  #define SPROX_API_CALL(a)  SPROXx_ ## a  

  /* Most functions need an instance parameter */
  #define SPROX_PARAM        SPROX_INSTANCE rInst, 
  #define SPROX_PARAM_V      SPROX_INSTANCE rInst
  #define SPROX_PARAM_P      rInst,
  #define SPROX_PARAM_PV     rInst
  
  /* This is the routine check on top of each function */
  #define SPROX_PARAM_TO_CTX SPROX_CTX_ST *sprox_ctx = (SPROX_CTX_ST *) rInst; if (sprox_ctx == NULL) return MI_INVALID_READER_CONTEXT;

  /* This is the low-level dialog function */
  #define SPROX_DLG_FUNC     SPROXx_Function
  #define SPROX_WRS_FUNC     SPROXx_FunctionWaitResp

#endif

#ifdef UNDER_CE
  #ifndef WIN32
    #define WIN32
  #endif
#endif

#ifdef __linux
  #ifndef LINUX
    #define LINUX
  #endif
#endif

/* We are building the library */
/* --------------------------- */
#ifdef WIN32

  /* Windows */
  #include <windows.h>
  #define SPRINGPROX_LIB __declspec( dllexport )
  
#else
 
  #include <termios.h>
  #include <unistd.h>  
  
  #ifndef MAX_PATH
  	#define MAX_PATH 256
  #endif

  #define _stprintf sprintf
  #define _tcscat strcat
  #define _tcsicmp strcmp
  #define _tcsncmp strncmp
  #define _tcsncpy strncpy
  #define _tcscpy strcpy
  #define _tcslen strlen
  #define _tcsclen strlen
  #define _tfopen fopen
  #define _ftprintf fprintf
  #define _T(x) x

  #define Sleep(x) usleep(1000*x)

#endif

#define UNUSED_PARAMETER(x) (void) x

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Main library header */
/* ------------------- */

#include "springprox.h"
#include "springprox_ex.h"

/* The internal context structure */
/* ------------------------------ */
struct _SPROX_CTX_ST
{
  struct
  {
    /* Protocol : default is osi */
    BYTE    brcd:1;
    BYTE    ascii:1;
    BYTE    bin:1;
    BYTE    bin_bus:1;
    BYTE    rs485:1;
    
    /* Baudrate : default is slow */
    BYTE    bdr_115200:1;
    
    /* Address on the bus */
    BYTE    bus_addr;
  } settings;

  struct
  {
    /* Force protocol ? */
    BYTE    osi:1;
    BYTE    ascii:1;
    BYTE    bin:1;
    BYTE    bin_bus:1;
    BYTE    rs485:1;
    
    /* Force baudrate ? */
    BYTE    bdr_38400:1;
    BYTE    bdr_115200:1;
    
  } want_settings;

  BYTE    com_sequence;
  BOOL    com_active;

#ifdef WIN32
  HANDLE  com_handle;
#else
  int     com_handle;
#endif
  DWORD   com_type;

  TCHAR   com_name[48 + 1];
  
  BOOL    com_reset_ctrl :1;
  BOOL    com_power_ctrl :1;
  BOOL    com_power_auto :1;
//  BOOL    com_is_usb_vcp :1;
//  BOOL    com_is_usb_cdc :1;
//  BOOL    com_is_usb_ftdi:1;
  
  BYTE    sprox_status;

  struct
  {
    DWORD   resp_tmo;
    DWORD   byte_tmo;
  }
  sprox_timeout;

  struct
  {
    BYTE    prd[4];
    BYTE    ver[3];
    BYTE    pid[5];
    BYTE    nid[4];
  } sprox_info;

  DWORD   sprox_version;
  DWORD   sprox_capabilities;
  
  /* Current RF operating mode */
  BYTE    current_mode;

  /* For Mifare functions */
  BYTE    mif_auth_ok;
  BYTE    mif_auth_info;
  BYTE    mif_snr[10];
  BYTE    mif_snr_len;

  /* For ISO 15693 functions */
  BYTE    iso15693_snr[8];
  BYTE    iso15693_mode;
  
  /* For ICODE1 functions */
  BYTE    i1_snr[8];
  BYTE    i1_mode;
};

typedef struct _SPROX_CTX_ST SPROX_CTX_ST;

/* Bit values for com_type */
#define DEVICE_POWER_AUTO    0x00010000
#define DEVICE_POWER_ON_DTR  0x00020000
#define DEVICE_RESET_ON_RTS  0x00040000
#define DEVICE_IS_COM        0x01000000
#define DEVICE_IS_CF         0x03000000
#define DEVICE_IS_SDIO       0x05000000
#define DEVICE_IS_FTDI       0x08000000


#ifndef SPROX_API_REENTRANT
  extern SPROX_CTX_ST *sprox_ctx_glob;
#endif

/* Project includes */
/* ---------------- */
#include "sprox_serial.h"
#include "sprox_dialog.h"
#include "sprox_14443-4.h"
#include "sprox_ver.h"
#include "sprox_capas.h"

#define SPROX_HIGH_BAUDRATE 1
extern DWORD DefaultForceBaudrate;

/* Low layer protocol */
/* ------------------ */
void  RecvFlush(SPROX_CTX_ST * sprox_ctx);

SWORD SendFrameBIN(SPROX_CTX_ST *sprox_ctx, const BYTE *buffer, WORD buflen);
SWORD RecvFrameBIN(SPROX_CTX_ST *sprox_ctx, BYTE *buffer, WORD *buflen);
SWORD SendFrameBUS(SPROX_CTX_ST *sprox_ctx, const BYTE *buffer, WORD buflen);
SWORD RecvFrameBUS(SPROX_CTX_ST *sprox_ctx, BYTE *buffer, WORD *buflen);
SWORD SendFrameOSI(SPROX_CTX_ST *sprox_ctx, const BYTE *buffer, WORD buflen);
SWORD RecvFrameOSI(SPROX_CTX_ST *sprox_ctx, BYTE *buffer, WORD *buflen);
SWORD SendFrameASC(SPROX_CTX_ST *sprox_ctx, const BYTE *buffer, WORD buflen);
SWORD RecvFrameASC(SPROX_CTX_ST *sprox_ctx, BYTE *buffer, WORD *buflen);



//BOOL IsCommUSB_FTDI(const TCHAR *device);
//BOOL IsCommUSB_CDC(const TCHAR *device);


/* Internal library functions */
/* -------------------------- */
SWORD SPROX_ReaderConnect(SPROX_CTX_ST *sprox_ctx);
SWORD SPROX_ReaderConnectAt(SPROX_CTX_ST *sprox_ctx, DWORD baudrate);
BOOL  GetDefaultDevice(SPROX_CTX_ST *sprox_ctx);
void  LoadSettings(void);
BOOL GetDefaultDevice_HW_Win32(SPROX_CTX_ST *sprox_ctx);
BOOL GetDefaultDevice_HW_WinCE(SPROX_CTX_ST *sprox_ctx);

/* Debug functions */
/* --------------- */

void SPROX_Trace(BYTE level, const char *fmt, ...);
BYTE SPROX_TraceGetLevel(void);
void SPROX_TraceSetLevel(BYTE level);
void SPROX_TraceSetFile(const TCHAR *filename);


SWORD SPROX_Brcd_Function(BYTE cmd, const BYTE send_buffer[], WORD send_bytelen, BYTE recv_buffer[], WORD *recv_bytelen);
SWORD SPROX_Brcd_ReaderOpen(const TCHAR device[], BOOL direct);

#define TRACE_DLG_LO    0x01
#define TRACE_DLG_HI    0x02
#define TRACE_ACCESS    0x04

#define TRACE_DEBUG     0x10
#define TRACE_INFOS     0x20
#define TRACE_WARNINGS  0x40
#define TRACE_ERRORS    0x80

#define TRACE_ALL       0xFF

#ifdef _DEBUG
	#define D(x) x
#else
	#define D(x)
#endif




#endif
